﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Bubbels.Controls
{
    /// <summary>
    /// Interaction logic for GameWindow.xaml
    /// </summary>
    public partial class GameWindow : Window
    {
        private int xPadding = 20;
        private int yPadding = 20;
        public GameWindow()
        {
            InitializeComponent();
            Uri iconUri = new Uri(@"pack://application:,,,/Resources/Title.PNG", UriKind.RelativeOrAbsolute);
            this.Icon = BitmapFrame.Create(iconUri);
        }
        public int Col = 11, Row = 11;

        private void NewGame_Click(object sender, RoutedEventArgs e)
        {
            this.NewGame(new BubbelsVM(Col, Row, (int)DateTime.Now.Ticks));
        }

        private void Restart_Click(object sender, RoutedEventArgs e)
        {
            BubbelsVM vm = this.DataContext as BubbelsVM;
            this.NewGame(new BubbelsVM(vm.Column, vm.Row, vm.Seed));
        }

        private void DumpGame_Click(object sender, RoutedEventArgs e)
        {
            // Dump game
        }

        private void ResizeWindow()
        {
            // Calculate out the most fitted ball size
            double ballSize = 70;
            double width = this.BorderThickness.Left + this.BorderThickness.Right + this.Col * ballSize + 15;
            if (width >  System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - xPadding)
            {
                width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - xPadding;
                ballSize = (width - (this.BorderThickness.Left + this.BorderThickness.Right + 15)) / this.Col;
            }

            double height = System.Windows.Forms.SystemInformation.ToolWindowCaptionHeight + 20 + this.Row * ballSize + this.BorderThickness.Top + this.BorderThickness.Bottom + 50;
            if (height > System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - yPadding)
            {
                height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - yPadding;
                ballSize = (height - (System.Windows.Forms.SystemInformation.ToolWindowCaptionHeight + this.BorderThickness.Top + this.BorderThickness.Bottom +70)) / this.Row;
                width = this.BorderThickness.Left + this.BorderThickness.Right + this.Col * ballSize + 15;

            }

            this.Height =height;
            this.Width = width;

        }

        private void ResizeBoard()
        {
            this.gameBoard.Width = this.Width - this.BorderThickness.Left - this.BorderThickness.Right - 15;
            this.gameBoard.Height = this.Height - this.BorderThickness.Top - this.BorderThickness.Bottom - 50;
        }

        public void NewGame(BubbelsVM blubbelsVM)
        {
            try
            {
                ResizeWindow();
                blubbelsVM.FillTable();
                this.DataContext = blubbelsVM;
                this.Col = blubbelsVM.Column;
                this.Row = blubbelsVM.Row;


            }
            catch (Exception ex)
            {
                
                throw;
            }
        }


        private void About_Click(object sender, RoutedEventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }
        private void CloseCommandExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.Close();
        }

        private void btnNewGame_Click(object sender, RoutedEventArgs e)
        {
            //this.mainGrid.Children.Remove(this.startGrid);
            this.gameBoard.Visibility = Visibility.Visible;
            this.NewGame_Click(sender, e);
        }

        private void HiddenGameControlPane()
        {
            this.gameControlPane.Visibility = Visibility.Hidden;
            this.gameBoard.Visibility = Visibility.Visible;
            this.gameBoard.Opacity = 1;
        }

        private void gameOverPanel_OnNewGame()
        {
            HiddenGameControlPane();
            this.NewGame_Click(null, null);
        }

        private void gameOverPanel_OnExitGame()
        {
            this.Close();
        }

        private void gameOverPanel_OnRestartGame()
        {
            HiddenGameControlPane();
            this.Restart_Click(null,null);
        }

        private void board_GameOver()
        {
            this.gameControlPane.EnableRestart(true);
            this.gameControlPane.Visibility = Visibility.Visible;
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // Check width and height of game, ensure no clip covered, refer http://blog.sina.com.cn/s/blog_962aaf5d010138yd.html
            int workingAreaHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            int workingAreaWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            //if (workingAreaHeight-this.yPadding <= this.Height)
            //{
            //    this.Height = workingAreaHeight - this.yPadding;
            //}
            //if (workingAreaWidth - xPadding <= this.Width)
            //{
            //    this.Width = workingAreaWidth - xPadding;
            //}

            // Move position
            if (this.Height+ this.Top > System.Windows.SystemParameters.PrimaryScreenHeight)
            {

                DoubleAnimation da = new DoubleAnimation((workingAreaHeight - this.Height) / 2, new Duration(new TimeSpan(0, 0, 1)));
                this.BeginAnimation(Window.TopProperty,da);
            }
            if (this.Width + this.Left > System.Windows.SystemParameters.PrimaryScreenWidth)
            {

                DoubleAnimation da = new DoubleAnimation((workingAreaWidth - this.Width) / 2, new Duration(new TimeSpan(0, 0, 1)));
                this.BeginAnimation(Window.LeftProperty, da);
            }
            this.ResizeBoard();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                // pause game and show control panel
                board_GameOver();
            }
        }
    }
}
